import pymysql


class DBUtils:
    __conn = None

    @classmethod
    def __get_conn(cls):
        if cls.__conn is None:
            cls.__conn = pymysql.connect(host="localhost", port=3306, user="root",
                                         password="root", database="ihrm", charset="utf8")

    @classmethod
    def __close_conn(cls):
        if cls.__conn is not None:
            cls.__conn.close()
            cls.__conn = None

    @classmethod
    def select_one_dblist(cls, sql):
        cursor = None
        res = None
        try:
            cls.__get_conn()
            cursor = cls.__conn.cursor()
            cursor.execute(sql)
            res = cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            cursor.close()
            cls.__close_conn()
            return res

    @classmethod
    def uid_dblist(cls, sql):
        cursor = None
        res = None
        try:
            cls.__get_conn()
            cursor = cls.__conn.cursor()
            cursor.execute(sql)
            res = cls.__conn.affected_rows()
            cls.__conn.commit()
        except Exception as e:
            print(e)
            cls.__conn.rollback()
        finally:
            cursor.close()
            cls.__close_conn()
            return res



